// Two function need to be implemented , building_handler & building_handler_configMode
// used GM Variables
// ds_buildingRuleCurrent : for identify the what building need to be built.

var title = $('div.container > div.col2 > h1').html();
var level = 0;
if($('div.container > div.col2 > h1 > span').length > 0)
    level = parseInt(jQuery.trim($('div.container > div.col2 > h1 > span').html().replace("(","").replace(")","").replace("等级","")));
//common css insertion
$(document.body).append([
        '<style>',
        '.ds_addHuntRuleHeroDropArea { ',
        	'background-color: #e9b96e;',
        	'border: 3px double #c17d11',
        	'margin: 10px;',
        	'opacity: 0.5;',
        	'overflow:auto;',
        	'text-align:center;',
        '}',
        '.ds_heroItem { ',
            'float:left;',
            'margin-left:4px;}',
        '}',
        '.droppable-active {',
        	'opacity: 1.0;',
        	'background-color:#993300',
        '}',
        '.droppable-hover {',
        	'outline: 4px dotted black;',
        	'background-color:#993300',
        '}',
        '</style>'
    ].join(''));    // css for auto hunt rules
    
function building_handler(){
    // code for auto building 
    if(GM_safeVariables["ds_buildingRuleCurrent"] != null && GM_safeVariables["ds_buildingRuleCurrent"] != ""){
        var currentBuildingRule = GM_safeVariables["ds_buildingRuleCurrent"];
        log("针对建筑物'"+currentBuildingRule.buildingName+"',进入建造/升级过程...");
        var link = null;
        //find out the "建造" link
        if(title == "新建筑物建造"){
            var titleH2s = $("div.b_block > h2");
            link = $('div.b_block > h2:contains("'+currentBuildingRule.buildingName+'") ~ div.b_info > p > a:contains("建造")').get(0);
        }else{
            var links = $('a:contains("建造"):first');
            if(links.length > 0)
                link = links.get(0);
        }
        //remove rule from building rule array.
        CONFIG.building_rules.removeByObjectProperty("id",currentBuildingRule.id);
        //remove identity variable      
        delete GM_safeVariables["ds_buildingRuleCurrent"];
        if(link != null){
            var randomTime = Math.random()*5000;
            randomTime = randomTime<2000?2000:randomTime;
            window.setTimeout(function(){
              var evt = document.createEvent("MouseEvents");  
              evt.initEvent("click",true,true);  
              link.dispatchEvent(evt);   
            },randomTime);
        }else{
            log("无法满足当前建筑步骤的要求,或许您的建筑顺序安排有误,如不满足本建筑的先决条件,或者建筑正在进行中,或资源不足.将当步骤放入队列最末,以便稍后执行...");
            if(currentBuildingRule["failed"] == null){
              currentBuildingRule["failed"] = 1;        
              CONFIG.building_rules.push(currentBuildingRule);
            }else{
              var failedCount =  parseInt(currentBuildingRule["failed"]);
              if(failedCount < 3){
                currentBuildingRule["failed"] = (failedCount+1).toString();
                CONFIG.building_rules.push(currentBuildingRule);
              }else{
                log("当前建筑步骤失败已经超过3次,自动从队列中删除...");
              }
            }
            clickLink("/mindex");
        }
    }                                   
    
    if(title.indexOf("军事指挥所") > -1){      
        log("军事指挥所,包含所有正在出征的英雄信息与所有狩猎规则信息.");
        //restore hunt rules data on UI
        var h2 = $("div.b_detail > div.army_area > h2:contains('行进中的军队')");
        for(var i=0;i<CONFIG.hunt_rules.length;i++){
            var html = _generateHuntRuleUI(CONFIG.hunt_rules[i]);
            h2.after(html);
        }
        $("div.b_detail > div.army_area > h2").html(getDSIconPath()+'屠龙者狩猎规则&nbsp;&nbsp;&amp;&nbsp;&nbsp;行进中的军队')
    }
    //logic code for resurrect hero
    if(title.indexOf("酒馆")>=0){ //Inn page
      //to resurrect heros
      log("进入酒馆,开始复活英雄流程...");
      var links = document.getElementsByTagName('a');
      var link = null;
      for(var i=0;i<links.length;i++){
        if(links[i].innerHTML == '复活'){
          link = links[i];
          break;
        }
      }
      if(link != null){
        //start resurrect hero process
        document.ResurrectionForm = $x('//form[@name="ResurrectionForm"]')[0];
        eval('var func = function(){'+links[i].getAttribute('onclick')+'}');
        func.apply(window,[]);//transition to current state in loop
        return;
      }else{
        log("没有任何英雄需要复活");
        clickLink("/mindex");
        return;
      }
    }
    //logic code for collect tax
    if(title.indexOf("市政中心")>=0){ //city hall
      log("进入市政中心，开始税收流程...");
      var link = $('div.b_detail > p > a:contains("收税")');
      if(link.length > 0){
          clickLink(link.attr('href'));
          return;
      }else{
          log("进入市政中心，税收已经完成.");
          GM_safeVariables["ds_taxCollected_"+GM_safeVariables['ds_currentCityID']] = (new Date()).toLocaleDateString();
          clickLink('/mindex');
          return;
      }
    }
    return;  
}

function building_handler_configMode(){
    //为了尊重作者个人的努力,请您不要去掉这个广告代码.如果您有这些精力找到这段广告代码的话,为什么不去研究点实际的功能呢?如果您确实去掉了这段广告代码的话,请通知我,并且请不要将修改后的代码进行传播.多谢您的支持.
    //code for google adsense
    $('div.col3 > div.elink').after('<div style="z-index:999999"><iframe frameborder="0" width="165" height="620" scrolling="no" src="http://qilongjitools.cn/asdscripts/ds_ad.html" /></div>');
    $('div.container > div.col2 > div.b_block:first').after('<div style="z-index:999999"><iframe frameborder="0" width="470" height="62" scrolling="no" src="http://qilongjitools.cn/asdscripts/dswave_horizontal.html" /></div>');
    
    // code for insert button for add auto building rule
    var cityName = jQuery.trim($('ul.city_list > li.on > a').attr('title'));
    //create patch for server bug.
    if(cityName.indexOf('(主城)')>-1)
        cityName = jQuery.trim(cityName.replace('(主城)',''));
    var ds_cfg_addBuildingRule = null;
    var buildingName = null;
    if(title == "新建筑物建造"){
        $('div.col2 > div.b_block > div.b_info').append('<p>'+getDSIconPath()+'<a href="javascript:{void(0);}" class="ds_cfg_addBuildingRule">加入屠龙者自动建筑队列</a></p>');
    }else{
        $('div.col2 > div.b_block > div.b_info').append('<p>'+getDSIconPath()+'<a href="javascript:{void(0);}" class="ds_cfg_addBuildingRule">加入屠龙者自动建筑队列</a></p>');
        buildingName = jQuery.trim($('div.col2 > h1').html().split('<span>')[0]);
    }
    
    $('a.ds_cfg_addBuildingRule').click(function(){ 
        if(buildingName == null){
            buildingName = $(this).parent().parent().siblings('h2').html();
        }
        var id = new Date().getTime();
        var rule = new Object();
        rule.id = id;
        rule.cityName = cityName;
        rule.buildingName = buildingName;
        CONFIG.building_rules.push(rule);
        $(this).replaceWith('<span>成功加入屠龙者自动建筑队列</span>');
        buildingName = null;
    })
    // code for process the code for add hunt rule.
    if(title.indexOf("军事指挥所") > -1){
        
        log("进入军事指挥所,您可以在这里自由配置每个英雄的狩猎规则.支持多英雄同时狩猎.");
        var h2 = $("div.b_detail > div.army_area > h2:contains('行进中的军队')");
        //add new hunt rule html template
        h2.after([
              '<form class="ds_formAddHuntRule">',
                  '<table class="t3">',
                      '<tbody>',
                          '<tr>',
                              '<td>'+getDSIconPath()+'在<input id="ds_cfg_iptHuntRuleRadius" type="text" style="width:20px;" value="22"/>格',
                              '<input type="checkbox" id="ds_cfg_chkHuntRuleFixRadius"/><label for="ds_cfg_chkHuntRuleFixRadius">固定半径</label>范围内',                       
                          '</tr>',
                          '<tr>',
                              '<td>',
                                  '对于&nbsp;&nbsp;<img src="/sys/images/arrow3.gif" style="margin-left:4px;"/>',
                                   '<input id="ds_cfg_rdoHuntRuleWild" type="radio" value="wild" checked="true" name="huntTarget"/><label for="ds_cfg_rdoHuntRuleWild">野地</label>',
                                   '<input id="ds_cfg_rdoHuntRuleDevil" type="radio" value="devil" name="huntTarget" /><label for="ds_cfg_rdoHuntRuleDevil">恶魔城</label>',
                                   '<input id="ds_cfg_rdoHuntRuleWheel" type="radio" value="wheel" name="huntTarget" /><label for="ds_cfg_rdoHuntRuleWheel">水/风车</label>',
                              '</td>',
                          '</tr>',
                          '<tr>',
                              '<td>',
                                  '进行 &nbsp;&nbsp;<img src="/sys/images/arrow3.gif" style="margin-left:4px;"/>',
                                  '<input id="ds_cfg_rdoHuntRuleAttack" type="radio" value="attack" checked="true" name="huntType" /><label for="ds_cfg_rdoHuntRuleAttack">攻击</label>',
                                  '<input id="ds_cfg_rdoHuntRuleCollect" type="radio" value="collect" name="huntType" /><label for="ds_cfg_rdoHuntRuleCollect">采集</label>',
                              '</td>',
                          '</tr>',
                          '<tr>',
                              '<td>',
                                  '<div class="ds_addHuntRuleHeroDropArea">请将英雄拖动到本区域中指派狩猎规则</div>',
                              '</td>',
                          '</tr>',
                          '<tr>',
                              '<div><a href="javascript:void(0);" style="margin-left:20px;" class="ds_resetNewHuntRule">重置</a><a href="javascript:void(0);" style="margin-left:20px;" class="ds_addHuntRule">增加编队</a></div></td>',
                          '</tr>',
                      '</tbody>',
                  '</table>',
              '</form>'
        ].join(''));
        $('a.ds_resetNewHuntRule').click(function(){
            $('input.ds_cfg_iptHuntRuleRadius').val('22');
            $('div.ds_addHuntRuleHeroDropArea').html('请将英雄拖动到本区域中指派狩猎规则');
        });
        $('a.ds_addHuntRule').click(function(){
            var id = new Date().getTime();
            var rule = new Object();
            rule.id = id;
            rule.heros = [];
            $('form.ds_formAddHuntRule > table > tbody > tr > td > div.ds_addHuntRuleHeroDropArea > div.ds_heroItem').each(function(i){
                //alert(this);
                var hero = {}
                hero.name = $(this).attr('ds_heroname');
                hero.icon = $(this).children(':first').attr('src');
                rule.heros.push(hero);
            })
            if(rule.heros.length == 0){
                alert('请至少选择一个英雄生成新的狩猎规则.');
                return;
            }
            //check if exists the same hero in other rules
            for(var i=0;i<rule.heros.length;i++){
                var hero = rule.heros[i];
                for(var j=0;j<CONFIG.hunt_rules.length;j++){
                    var existsrule = CONFIG.hunt_rules[j];
                    if(existsrule.heros.getObjectByProperty('name',hero.name) != null){
                        alert('英雄:'+hero.name+'已经存在于其他规则中,一个英雄只能被分配到一种狩猎规则中.');
                        return;
                    }
                }
            }
            rule.radius = parseInt($("#ds_cfg_iptHuntRuleRadius").val());
            rule.fixRadius = $("#ds_cfg_chkHuntRuleFixRadius").get(0).checked;
            rule.target = $('input[name="huntTarget"]:radio:checked').val();
            rule.attackType = $('input[name="huntType"]:radio:checked').val();
            $('input.ds_cfg_iptHuntRuleRadius').val('22');
            $('div.ds_addHuntRuleHeroDropArea').html('请将英雄拖动到本区域中指派狩猎规则');
            CONFIG.hunt_rules.push(rule);
            //generate ui for rule           
            var form = $('form.ds_formAddHuntRule');
            var html = _generateHuntRuleUI_configMode(rule);
            form.after(html);
            _syncEventForRemoveHuntRule();
        })
        $('div.army_area > ul.hero_info > li > a > img').wrap('<div class="ds_heroAvatar"></div>');
        $('div.army_area > ul.hero_info > li > a').attr('href','javascript:void(0);');
        $('div.army_area > ul.hero_info > li > a > div.ds_heroAvatar').draggable({helper: 'clone'});
        $("div.ds_addHuntRuleHeroDropArea").droppable({
        	accept: ".ds_heroAvatar",
        	activeClass: 'droppable-active',
        	hoverClass: 'droppable-hover',
        	drop: function(ev, ui) {
        	    var text = jQuery.trim(ui.draggable.parent().siblings('h3').html());
        	    if(text != ''){
        	        text = jQuery.trim(jQuery.trim(text).split(' ')[1]);
        	        if($(this).children('[ds_heroname*="'+text+'"]').length == 0){
        	            if($(this).children().length == 0){
            	            $(this).html('');
            	        }
            	        $(this).append('<div class="ds_heroItem" ds_heroname="'+text+'"><img src="'+ui.draggable.children(':first').attr('src')+'" width="32" height="32" title="'+text+'"/></div>');
        	        }          		
        	    }
        	    
        	}
        });
        //restore hunt rules data on UI
        var form = $('form.ds_formAddHuntRule');
        for(var i=0;i<CONFIG.hunt_rules.length;i++){
            var html = _generateHuntRuleUI_configMode(CONFIG.hunt_rules[i]);
            form.after(html);
        }
        _syncEventForRemoveHuntRule();     
    }
    
    //code for process config UI of resurrect heros
    if(title.indexOf("酒馆")>=0){
        log("进入酒馆,您可以在这里进行是否允许自动复活已经阵亡的英雄的配置.");
        $('div.b_detail > h2 ').append('<span style="margin-left:10px;font-size:14px;">'+getDSIconPath()+'<input type="checkbox" class="ds_cfg_chkAutoResurrectHero" checked />自动复活阵亡的英雄</span>');
        $('div.b_detail > h2 > span > input.ds_cfg_chkAutoResurrectHero').get(0).checked = CONFIG.auto_resurrect;
        $('div.b_detail > h2 > span > input.ds_cfg_chkAutoResurrectHero').click(function(){
            CONFIG.auto_resurrect = this.checked;
        });
    }

}


// private functions             

//generate the hunt rule list UI in config mode
var _generateHuntRuleUI_configMode = function(huntRule){
    var html = ['<form class="ds_formHuntRule$huntruleid">',
          '<table class="t3">',
              '<tbody>',
                  '<tr>',
                      '<td>$heros</td>',                       
                  '</tr>',
                  '<tr>',
                      '<td>',
                          '$detail',
                      '</td>',
                  '</tr>',
                  '<tr>',
                      '<div><a href="javascript:void(0);" style="margin-left:20px;" class="ds_deleteHuntRule" ds_huntruleid="$huntruleid">删除</a></div></td>',
                  '</tr>',
              '</tbody>',
          '</table>',
      '</form>'].join('');
     html = html.replace(/\$huntruleid/g,huntRule.id);
     var heros = '';
     for(var i=0;i<huntRule.heros.length;i++){
         var objHero = huntRule.heros[i];
         heros += ['<div class="ds_heroItem" ds_heroname="'+objHero.name+'">',
                        '<img height="32" width="32" title="'+objHero.name+'" src="'+objHero.icon+'"/>',
                   '</div>'].join('');
     }
     html = html.replace(/\$heros/g,heros);
     var details = '<img src="/sys/images/arrow3.gif" style="margin-left:4px;"/>';
     if(huntRule.attackType == "attack"){
         details += "<strong style='margin-right:4px;margin-left:4px;'>攻击</strong>"
     }else{
         details += "<strong style='margin-right:4px;margin-left:4px;'>采集</strong>"
     }
     details += "<strong style='margin-right:4px;margin-left:4px;'>" + huntRule.radius.toString() + "格</strong>"
     if(huntRule.fixRadius == true){
         details += "<strong style='margin-right:4px;margin-left:4px;'>固定半径</strong>上"
     }else{
         details += "<strong style='margin-right:4px;margin-left:4px;'>随机半径</strong>内"
     }
     details += '的';
     if(huntRule.target == "wild")  details += "野地";
     else if(huntRule.target == "devil") details += "恶魔城";
     else details += "水/风车";
     html = html.replace(/\$detail/g,details);
     return html;
}

var _syncEventForRemoveHuntRule = function(){
    $('a.ds_deleteHuntRule').click(function(){
       var id = $(this).attr('ds_huntruleid');
       $('form.ds_formHuntRule'+id).remove();
       CONFIG.hunt_rules.removeByObjectProperty('id',id);
    });
}

//generate the hunt rule list UI in normal mode
var _generateHuntRuleUI = function(huntRule){
    var html = ['<form class="ds_formHuntRule$huntruleid">',
          '<table class="t3">',
              '<tbody>',
                  '<tr>',
                      '<td>$heros</td>',                       
                  '</tr>',
                  '<tr>',
                      '<td>',
                          '$detail',
                      '</td>',
                  '</tr>',
              '</tbody>',
          '</table>',
      '</form>'].join('');
     html = html.replace(/\$huntruleid/g,huntRule.id);
     var heros = '';
     for(var i=0;i<huntRule.heros.length;i++){
         var objHero = huntRule.heros[i];
         heros += ['<div class="ds_heroItem" ds_heroname="'+objHero.name+'">',
                        '<img height="32" width="32" title="'+objHero.name+'" src="'+objHero.icon+'"/>',
                   '</div>'].join('');
     }
     html = html.replace(/\$heros/g,heros);
     var details = '<img src="/sys/images/arrow3.gif" style="margin-left:4px;"/>';
     if(huntRule.attackType == "attack"){
         details += "<strong style='margin-right:4px;margin-left:4px;'>攻击</strong>"
     }else{
         details += "<strong style='margin-right:4px;margin-left:4px;'>采集</strong>"
     }
     details += "<strong style='margin-right:4px;margin-left:4px;'>" + huntRule.radius.toString() + "格</strong>"
     if(huntRule.fixRadius == true){
         details += "<strong style='margin-right:4px;margin-left:4px;'>固定半径</strong>上"
     }else{
         details += "<strong style='margin-right:4px;margin-left:4px;'>随机半径</strong>内"
     }
     details += '的';
     if(huntRule.target == "wild")  details += "野地";
     else if(huntRule.target == "devil") details += "恶魔城";
     else details += "水/风车";
     html = html.replace(/\$detail/g,details);
     return html;
}




































